package com.bdsys.serviceimpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bdsys.dao.MessageDAO;
import com.bdsys.dao.UserDAO;
import com.bdsys.domain.MessageDomain;
import com.bdsys.domain.UserDomain;
import com.bdsys.service.MessageService;
import com.bdsys.servicedto.MessageDetailDTO;
import com.bdsys.servicedto.MessageServiceDTO;
import com.bdsys.servicedto.UserDetailDTO;
import com.bdsys.servicedto.UserServiceDTO;

public class MessageServiceImpl implements MessageService {

	private MessageDAO messageDAO;

	public MessageDAO getMessageDAO() {
		return messageDAO;
	}

	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}

	@Override
	public void addMessage(MessageServiceDTO inServiceDTO) {
		MessageDomain messageDomain = this.fromDetailDTOToDomain(inServiceDTO
				.getMessageDetailDTO());
		messageDAO.addMessage(messageDomain);
	}

	private MessageDomain fromDetailDTOToDomain(MessageDetailDTO inDetailDTO) {
		MessageDomain messageDomain = new MessageDomain();
		messageDomain.setContent(inDetailDTO.getContent());
		messageDomain.setId(inDetailDTO.getId());
		messageDomain.setIsRead(inDetailDTO.getIsRead());
		messageDomain.setIsRecieverDelete(inDetailDTO.getIsRecieverDelete());
		messageDomain.setRecieverId(inDetailDTO.getRecieverId());
		messageDomain.setIsSenderDelete(inDetailDTO.getIsSenderDelete());
		messageDomain.setRecieverId(inDetailDTO.getRecieverId());
		messageDomain.setSenderId(inDetailDTO.getSenderId());
		messageDomain.setSendTime(inDetailDTO.getSendTime());
		messageDomain.setTitle(inDetailDTO.getTitle());
		return messageDomain;
	}

	@Override
	public MessageServiceDTO selectMessagesBySenderId(String senderId) {
		MessageDomain inDomain = new MessageDomain();
		inDomain.setSenderId(senderId);
		List<MessageDomain> messages = messageDAO
				.selectMessagesByConditions(inDomain);
		List<MessageDetailDTO> list = new ArrayList<MessageDetailDTO>();
		for (MessageDomain domain : messages) {
			list.add(this.fromDomainTODetailDTO(domain));
		}

		MessageServiceDTO outServiceDTO = new MessageServiceDTO();
		outServiceDTO.setList(list);
		return outServiceDTO;
	}

	private MessageDetailDTO fromDomainTODetailDTO(MessageDomain messageDomain) {
		MessageDetailDTO messageDetailDTO = new MessageDetailDTO();
		messageDetailDTO.setContent(messageDomain.getContent());
		messageDetailDTO.setContent(messageDomain.getContent());
		messageDetailDTO.setId(messageDomain.getId());
		messageDetailDTO.setIsRead(messageDomain.getIsRead());
		messageDetailDTO.setIsRecieverDelete(messageDomain
				.getIsRecieverDelete());
		messageDetailDTO.setRecieverId(messageDomain.getRecieverId());
		messageDetailDTO.setIsSenderDelete(messageDomain.getIsSenderDelete());
		messageDetailDTO.setRecieverId(messageDomain.getRecieverId());
		messageDetailDTO.setSenderId(messageDomain.getSenderId());
		messageDetailDTO.setSendTime(messageDomain.getSendTime());
		messageDetailDTO.setTitle(messageDomain.getTitle());
		return messageDetailDTO;
	}

	@Override
	public MessageServiceDTO selectMessagesByRecieverId(String recieverId) {
		MessageDomain inDomain = new MessageDomain();
		inDomain.setRecieverId(recieverId);
		List<MessageDomain> messages = messageDAO
				.selectMessagesByConditions(inDomain);
		List<MessageDetailDTO> list = new ArrayList<MessageDetailDTO>();
		for (MessageDomain domain : messages) {
			list.add(this.fromDomainTODetailDTO(domain));
		}

		MessageServiceDTO outServiceDTO = new MessageServiceDTO();
		outServiceDTO.setList(list);
		return outServiceDTO;
	}

	@Override
	public MessageServiceDTO selectMessagesByConditions(
			MessageServiceDTO inServiceDTO) {
		
		MessageDomain inDomain = this.fromDetailDTOToDomain(inServiceDTO.getMessageDetailDTO());
		List<MessageDomain> messages = messageDAO.selectMessagesByConditions(inDomain);
		
		List<MessageDetailDTO> list = new ArrayList<MessageDetailDTO>();
		for (MessageDomain domain : messages) {
			list.add(this.fromDomainTODetailDTO(domain));
		}

		MessageServiceDTO outServiceDTO = new MessageServiceDTO();
		outServiceDTO.setList(list);
		return outServiceDTO;
	}

	@Override
	public void deleteMessage(String id) {
		messageDAO.deleteMessage(id);
	}

	/**
	 * 更新字段，让邮件不在发送者的发件箱中出现
	 */
	@Override
	public void deleteSenderMessage(String id) {

	}

	@Override
	public void deleteRevieverMessage(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateMessage(MessageServiceDTO inServiceDTO) {
		// TODO Auto-generated method stub

	}
}
